<?php

namespace addons\booking\model;
use addons\booking\model\StoreShield;
use addons\booking\model\StoreOrder as StoreOrderModel;
use think\Model;


class Worker extends Model
{


    // 表名
    protected $name = 'worker';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    //获取附近的保洁
    public static function vicinity_wroker($store_id = 0,$page=1,$limit=10,$order='asc',$keyword='')
    {
        $store_info = Store::where('id',$store_id)->find();
        if(!$store_info) return [];
        // 数据库字段名 - 纬度
        $field_lat = 'latitude';
        // 数据库字段名 - 经度
        $field_lng = 'longitude';

        $where = [];
        $where['status'] = 1;
        $where['examine_status'] = 1;
        $where['is_work'] = 1;
        $pageNum = 20;
        if (isset($limit) && !empty($limit)) {
            $pageNum = $limit;
        }

        $list = self::where($where)
            ->where(function ($query) use ($keyword) {
                if ($keyword) {
                    $query->whereOr('user_name', 'like', '%' . $keyword . '%')
                        ->whereOr('mobile', 'like', '%' . $keyword . '%');
                }
            })
            ->field("id,user_name,head_image,mobile,work_status,order_num,order_room_num,score, (6378.138 * 2 * asin(sqrt(pow(sin(({$field_lat} * pi() / 180 - {$store_info['latitude']} * pi() / 180) / 2),2) + cos({$field_lat} * pi() / 180) * cos({$store_info['latitude']} * pi() / 180) * pow(sin(({$field_lng} * pi() / 180 - {$store_info['longitude']} * pi() / 180) / 2),2))) * 1000) as distance")
            ->order('distance '.$order)
            // 按距离升序排列（由近到远
            ->paginate($pageNum)->toArray();
        foreach ($list['data'] as $k=>$v){
            $distance =  number_format($v['distance'] / 1000, 1);
            $list['data'][$k]['distance'] = $distance;
        }

        return $list;
    }

    //获取附近的保洁
    public static function wroker_list($store_id = 0,$page=1,$limit=10,$order='asc',$keyword='',$type=1)
    {
        $store_info = Store::where('id',$store_id)->find();
        if(!$store_info) return [];
        // 数据库字段名 - 纬度
        $field_lat = 'latitude';
        // 数据库字段名 - 经度
        $field_lng = 'longitude';

        $where = [];
        $where['status'] = 1;
        $where['examine_status'] = 1;
        $where['is_work'] = 1;
        $pageNum = 20;
        if (isset($limit) && !empty($limit)) {
            $pageNum = $limit;
        }

        if($type == 2) {
            $sh_where['store_id'] = $store_id;
            $sh_where['type'] = 1;
            $ids =  StoreShield::where($sh_where)->column('worker_id');
            if($ids){
                $where['id'] = ['in',$ids];
            } else {
                return [];
            }
        } else if($type == 3) {
            $sh_where['store_id'] = $store_id;
            $sh_where['type'] = 2;
            $ids =  StoreShield::where($sh_where)->column('worker_id');
            if($ids){
                $where['id'] = ['in',$ids];
            } else {
                return [];
            }
        }

        $list = self::where($where)
            ->where(function ($query) use ($keyword) {
                if ($keyword) {
                    $query->whereOr('user_name', 'like', '%' . $keyword . '%')
                        ->whereOr('mobile', 'like', '%' . $keyword . '%');
                }
            })
            ->field("id,user_name,head_image,mobile,work_status,order_num,order_room_num,score, (6378.138 * 2 * asin(sqrt(pow(sin(({$field_lat} * pi() / 180 - {$store_info['latitude']} * pi() / 180) / 2),2) + cos({$field_lat} * pi() / 180) * cos({$store_info['latitude']} * pi() / 180) * pow(sin(({$field_lng} * pi() / 180 - {$store_info['longitude']} * pi() / 180) / 2),2))) * 1000) as distance")
            ->order('distance '.$order)
            // 按距离升序排列（由近到远
            ->paginate($pageNum)->toArray();
        foreach ($list['data'] as $k=>$v){
            $distance =  number_format($v['distance'] / 1000, 1);
            $list['data'][$k]['distance'] = $distance;
        }

        return $list;
    }

    //获取保洁个人信息
    public static function get_worker_info($worker_id = 0 )
    {
        $info = self::where('id',$worker_id)->find();
        return $info;
    }

    //修改保洁上下班
    public static function set_work_status($worker_id)
    {
        $worker = self::where('id',$worker_id)->find();
        $map = [];
        if($worker['work_status'] == 0){
            $map['work_status'] = 1;
        } else {
            $map['work_status'] = 0;
        }

        $res = self::where('id',$worker_id)->update($map);
        return $res;
    }

}
